<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="" prop="returnStartDate">
        <el-date-picker
          clearable
          v-model="queryParams.returnStartDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择退货计算开始日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="" prop="mainWarehouseName">
        <el-input
          v-model="queryParams.mainWarehouseName"
          placeholder="请输入主仓仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="allocationMode">
        <el-input
          v-model="queryParams.allocationMode"
          placeholder="请输入分配模式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['productAllocation:purAllocConfig:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['productAllocation:purAllocConfig:edit']"
          >修改</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['productAllocation:purAllocConfig:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['productAllocation:purAllocConfig:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purAllocConfigList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column
        label="退货计算开始日期"
        align="center"
        prop="returnStartDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnStartDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="上架计算开始日期"
        align="center"
        prop="shelfStartTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shelfStartTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="主仓仓库名称"
        align="center"
        prop="mainWarehouseName"
      >
      </el-table-column>
      <el-table-column
        label="店铺仓库关系"
        align="center"
        prop="allocationMode"
      >
        <template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="selectformisNew(scope.row.mainWarehouseName)"
            >店铺仓库关系</span
          >
        </template>
      </el-table-column>
      <el-table-column label="钉钉通知" align="center" prop="allocationMode">
        <template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="handleDingding"
            >钉钉通知绑定关系</span
          >
        </template>
      </el-table-column>
      <!-- <el-table-column label="分配模式" align="center" prop="allocationMode">
        <template slot-scope="scope">
          <div>
            {{ scope.row.allocationMode | state1 }}
          </div>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['productAllocation:purAllocConfig:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['productAllocation:purAllocConfig:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购分货配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="退货计算开始日期" prop="returnStartDate">
          <el-date-picker
            clearable
            v-model="form.returnStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退货计算开始日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="上架计算开始日期" prop="shelfStartTime">
          <el-date-picker
            clearable
            v-model="form.shelfStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上架计算开始日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="主仓仓库名称" prop="mainWarehouseName">
          <!-- <el-input
            v-model="form.mainWarehouseName"
            placeholder="请输入主仓仓库名称"
          /> -->
          <el-select
            v-model="form.mainWarehouseName"
            clearable
            filterable
            placeholder="仓库"
          >
            <el-option
              v-for="item in WarehouseList"
              :key="item.comment"
              :label="`${item.comment}`"
              :value="item.comment"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="分配模式" prop="allocationMode">
          
          <el-radio-group
            v-model="form.allocationMode"
            size="small"
            fill="#66b1ff"
          >
            <el-radio :label="0">手工</el-radio>
            <el-radio :label="1">自动</el-radio>
          </el-radio-group>
          <el-button
            style="margin-left: 10px"
            v-if="form.allocationMode == 1"
            type="primary"
            size="mini"
            @click="handlebatchEdit"
            >店铺设置</el-button
          >
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="openbatchEdit" v-if="openbatchEdit">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain size="mini" @click="submitbatchEdit"
            >确认</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-checkbox
            true-label="0"
            false-label="1"
            v-model="isAll"
            @change="changeisAll"
            >全选</el-checkbox
          >
        </el-col>
      </el-row>
      <el-table :data="BiStoreWarehouseBindingList">
        <el-table-column label="是否自动" align="center" prop="allocationMode">
          <template slot-scope="scope">
            <el-checkbox
              true-label="0"
              false-label="1"
              v-model="scope.row.allocationMode"
            ></el-checkbox
          ></template>
        </el-table-column>
        <el-table-column label="店铺名称" align="center" prop="storeName" />
      </el-table>
    </el-dialog>

    <el-dialog :visible.sync="openWarehouse" width="1800px">
      <BiStoreWarehouseBinding :mainWarehouseName="mainWarehouseName" />
    </el-dialog>

    <el-dialog :visible.sync="openInfo" width="1800px">
      <BiWarehouseDingdingBinding />
    </el-dialog>
  </div>
</template>

<script>
import {
  listPurAllocConfig,
  getPurAllocConfig,
  delPurAllocConfig,
  addPurAllocConfig,
  updatePurAllocConfig,
} from "@/api/productAllocation/purAllocConfig";
import {
  listBiStoreWarehouseBinding,
  getBiStoreWarehouseBinding,
  delBiStoreWarehouseBinding,
  addBiStoreWarehouseBinding,
  updateBiStoreWarehouseBinding,
  BiShopWarehouseRelation,
  JstShopInfo,
  batchEdit,
} from "@/api/conf/BiStoreWarehouseBinding";

import BiWarehouseDingdingBinding from "@/views/productAllocation/BiWarehouseDingdingBinding/index.vue";
import BiStoreWarehouseBinding from "@/views/conf/BiStoreWarehouseBinding/index.vue";
export default {
  name: "purAllocConfig",
  components: {
    BiStoreWarehouseBinding,
    BiWarehouseDingdingBinding,
  },
  data() {
    return {
      openInfo: false,
      isAll: "1",
      openbatchEdit: false,
      BiStoreWarehouseBindingList: [],
      openWarehouse: false,
      mainWarehouseName: 0,
      WarehouseList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购分货配置表格数据
      purAllocConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnStartDate: null,
        mainWarehouseName: null,
        allocationMode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  filters: {
    // 0表示手工，1表示自动
    state1(string) {
      if (string == "0") {
        return "手工";
      }
      if (string == "1") {
        return "自动";
      }
    },
  },
  created() {
    this.getList();
    this.selectBiShopWarehouseRelation();
  },
  methods: {
    changeisAll() {
      if (this.isAll == "0") {
        this.BiStoreWarehouseBindingList.forEach((item) => {
          item.allocationMode = "0";
        });
      } else {
        this.BiStoreWarehouseBindingList.forEach((item) => {
          item.allocationMode = "1";
        });
      }
    },
    submitbatchEdit() {
      // let list = [];
      // this.BiStoreWarehouseBindingList.forEach((item) => {
      //   if (item.allocationMode == "0") {
      //     list.push(item);
      //   }
      // });
      batchEdit(this.BiStoreWarehouseBindingList).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess(response.msg);
        }
        this.openbatchEdit = false;
        this.getList();
      });
    },
    handlebatchEdit() {
      listBiStoreWarehouseBinding({
        pageNum: 1,
        pageSize: 100,
      }).then((response) => {
        this.BiStoreWarehouseBindingList = response.rows;
        this.openbatchEdit = true;
      });
    },
    handleDingding() {
      this.openInfo = true;
    },
    selectformisNew(mainWarehouseName) {
      this.mainWarehouseName++;
      this.openWarehouse = true;
    },
    selectBiShopWarehouseRelation() {
      BiShopWarehouseRelation({ pageNum: 1, pageSize: 100 }).then(
        (response) => {
          this.WarehouseList = response.rows.map((item) => ({
            columnName: item.warehouseId,
            comment: item.warehouseName,
          }));
        }
      );
    },
    /** 查询采购分货配置列表 */
    getList() {
      this.loading = true;
      listPurAllocConfig(this.queryParams).then((response) => {
        this.purAllocConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        returnStartDate: null,
        mainWarehouseName: null,
        allocationMode: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购分货配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPurAllocConfig(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购分货配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePurAllocConfig(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurAllocConfig(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除采购分货配置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPurAllocConfig(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "productAllocation/purAllocConfig/export",
        {
          ...this.queryParams,
        },
        `purAllocConfig_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
